package com.dongpl.config.start_up;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

/**
 * 启动项目启动
 */
@Configuration
public class startClass implements CommandLineRunner {

    @Value("${openProject.isOpen}")
    private boolean isOpen;

    @Value("${openProject.web.openUrl}")
    private String openUrl;

    @Value("${openProject.cmd}")
    private String cmd;

    @Override
    public void run(String... args) throws Exception {
        if(isOpen){
            //获取操作系统的名字
            String osName = System.getProperty("os.name", "");
            try {
                if (osName.startsWith("Mac OS")) {
                    @SuppressWarnings("rawtypes")
                    Class fileMgr = Class.forName("com.apple.eio.FileManager");
                    Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
                    openURL.invoke(null, new Object[] { openUrl });
                } else if (osName.startsWith("Windows")) {
                    Runtime.getRuntime().exec(new String[] { cmd, openUrl });
                } else { // assume Unix or Linux, deepin 默认使用browser
                    String[] browsers = { "browser", "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
                    String browser = null;
                    for (int count = 0; count < browsers.length && browser == null; count++) {
                        if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) {
                            browser = browsers[count];
                        }
                    }
                    if (browser != null) {
                        Runtime.getRuntime().exec(new String[] { browser, openUrl });
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}
